###
 # @Author: szx
 # @Date: 2021-12-12 14:34:28
 # @LastEditTime: 2021-12-12 15:34:51
 # @Description: 
 # @FilePath: /couchdb-hastings-3.1.1/search_clouseau/offical_searchOfGeo.sh
### 

# 创建表
curl -X PUT admin:adminpw@localhost:5984/city

sleep 1
# 创建搜索索引，具体创建格式参考官方文档：https://docs.couchdb.org/en/3.1.1/ddocs/search.html#ddoc-search
curl -X POST -H "Content-Type: application/json" -d '
{
  "_id": "_design/trymesoft",
  "indexes" : {
    "shelter_search": {
      "index" : "function(doc) { if(doc.type && doc.type=='"'city'"') { index('"'city'"',doc.name,{'"'store'"':true}); index('"'lat'"',doc.lat,{'"'store'"':true}); index('"'lon'"',doc.lon,{'"'store'"':true}); } }"
    }
  }
}' admin:adminpw@localhost:5984/city

sleep 1
# 添加数据
curl -X POST -H "Content-Type: application/json" -d '
{
    "name":"Aberdeen, Scotland",
    "lat":56.15,
    "lon":-2.15,
    "type":"city"
}
' admin:adminpw@localhost:5984/city

sleep 1
curl -X POST -H "Content-Type: application/json" -d '
{
    "name":"Aberdeen, Scotland",
    "lat":55.15,
    "lon":-2.15,
    "type":"city"
}
' admin:adminpw@localhost:5984/city

sleep 1
# 查询空间属性，具体使用方法查看 https://docs.couchdb.org/en/3.1.1/ddocs/search.html#ddoc-search-geographical-searches
curl -X POST -H "Content-Type: application/json" -d '
{
    "q":"*:*",
    "sort":"<distance,lon,lat,-2.15,57.15,km>"
}' admin:adminpw@localhost:5984/city/_design/trymesoft/_search/shelter_search
